package com.gong.springbook;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RequestMapping("/book")
@RestController
public class BookController {
    @RequestMapping("/getList")
    public List<BookInfo> getList() {
        //1.从数据库中获取数据
        //采用数据Mock的方式
        List<BookInfo> bookInfos = mockBookData();
        //2.对金桔进行处理，比如状态转换
        for(BookInfo bookInfo : bookInfos) {
            if(bookInfo.getState() == 1) {
                bookInfo.setStateCN("可借阅");
            }else if(bookInfo.getState() == 2) {
                bookInfo.setStateCN("不可借阅");
            }
        }
        //3.返回数据
        return bookInfos;
    }

    public List<BookInfo> mockBookData() {
        List<BookInfo> bookInfos = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(i);
            bookInfo.setBookName("图书"+ i);
            bookInfo.setAuthor("作者" + i);
            bookInfo.setCount(i*15 + 3);
            bookInfo.setPrice(new BigDecimal(new Random().nextInt(100)));
            bookInfo.setState(i%5 == 0?2:1);
            bookInfos.add(bookInfo);
        }
        return bookInfos;
    }

}
